package com.openbravo.possync;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.customers.CustomerInfoExt;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.DataLogicSystem;
import com.openbravo.pos.forms.ProcessAction;
import com.openbravo.pos.payment.PaymentInfo;
import com.openbravo.pos.ticket.TaxInfo;
import com.openbravo.pos.ticket.TicketInfo;
import com.openbravo.pos.ticket.TicketLineInfo;
import com.openbravo.ws.externalsales.BPartner;
import com.openbravo.ws.externalsales.Order;
import com.openbravo.ws.externalsales.OrderIdentifier;
import com.openbravo.ws.externalsales.OrderLine;
import com.openbravo.ws.externalsales.Payment;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.xml.rpc.ServiceException;

public class OrdersSync
  implements ProcessAction
{
  private DataLogicSystem dlsystem;
  private DataLogicIntegration dlintegration;
  private ExternalSalesHelper externalsales;

  public OrdersSync(DataLogicSystem paramDataLogicSystem, DataLogicIntegration paramDataLogicIntegration)
  {
    this.dlsystem = paramDataLogicSystem;
    this.dlintegration = paramDataLogicIntegration;
    this.externalsales = null;
  }

  public MessageInf execute()
    throws BasicException
  {
    try
    {
      if (this.externalsales == null)
        this.externalsales = new ExternalSalesHelper(this.dlsystem);
      List localList = this.dlintegration.getTickets();
      Object localObject = localList.iterator();
      while (((Iterator)localObject).hasNext())
      {
        TicketInfo localTicketInfo = (TicketInfo)((Iterator)localObject).next();
        localTicketInfo.setLines(this.dlintegration.getTicketLines(localTicketInfo.getId()));
        localTicketInfo.setPayments(this.dlintegration.getTicketPayments(localTicketInfo.getId()));
      }
      if (localList.size() == 0)
        return new MessageInf(-67108864, AppLocal.getIntString("message.zeroorders"));
      Order[]   localObject2 = transformTickets(localList);
      if (!this.externalsales.uploadOrders((Order[]) localObject))
        throw new BasicException(AppLocal.getIntString("message.returnnull"));
      this.dlintegration.execTicketUpdate();
      return new MessageInf(-83886080, AppLocal.getIntString("message.syncordersok"), AppLocal.getIntString("message.syncordersinfo", new Object[] { Integer.valueOf(localObject2.length) }));
    }
    catch (ServiceException localServiceException)
    {
      throw new BasicException(AppLocal.getIntString("message.serviceexception"), localServiceException);
    }
    catch (RemoteException localRemoteException)
    {
      throw new BasicException(AppLocal.getIntString("message.remoteexception"), localRemoteException);
    }
    catch (MalformedURLException localMalformedURLException)
    {
    	   throw new BasicException(AppLocal.getIntString("message.malformedurlexception"), localMalformedURLException);

    }
   }

  private Order[] transformTickets(List<TicketInfo> paramList)
  {
    Order[] arrayOfOrder = new Order[paramList.size()];
    for (int i = 0; i < paramList.size(); i++)
    {
      TicketInfo localTicketInfo = (TicketInfo)paramList.get(i);
      arrayOfOrder[i] = new Order();
      OrderIdentifier localOrderIdentifier = new OrderIdentifier();
      Calendar localCalendar = Calendar.getInstance();
      localCalendar.setTime(localTicketInfo.getDate());
      localOrderIdentifier.setDateNew(localCalendar);
      localOrderIdentifier.setDocumentNo(Integer.toString(localTicketInfo.getTicketId()));
      arrayOfOrder[i].setOrderId(localOrderIdentifier);
      arrayOfOrder[i].setState(800175);
      BPartner localBPartner;
      if (localTicketInfo.getCustomerId() == null)
      {
        localBPartner = null;
      }
      else
      {
        localBPartner = new BPartner();
        localBPartner.setId(localTicketInfo.getCustomer().getSearchkey());
        localBPartner.setName(localTicketInfo.getCustomer().getName());
      }
      arrayOfOrder[i].setBusinessPartner(localBPartner);
      OrderLine[] arrayOfOrderLine = new OrderLine[localTicketInfo.getLines().size()];
      for (int j = 0; j < localTicketInfo.getLines().size(); j++)
      {
        TicketLineInfo localTicketLineInfo = (TicketLineInfo)localTicketInfo.getLines().get(j);
        arrayOfOrderLine[j] = new OrderLine();
        arrayOfOrderLine[j].setOrderLineId(String.valueOf(localTicketLineInfo.getTicketLine()));
        if (localTicketLineInfo.getProductID() == null)
          arrayOfOrderLine[j].setProductId("0");
        else
          arrayOfOrderLine[j].setProductId(localTicketLineInfo.getProductID());
        arrayOfOrderLine[j].setUnits(localTicketLineInfo.getMultiply());
        arrayOfOrderLine[j].setPrice(localTicketLineInfo.getPrice());
        arrayOfOrderLine[j].setTaxId(localTicketLineInfo.getTaxInfo().getId());
      }
      arrayOfOrder[i].setLines(arrayOfOrderLine);
      Payment[] arrayOfPayment = new Payment[localTicketInfo.getPayments().size()];
      for (int k = 0; k < localTicketInfo.getPayments().size(); k++)
      {
        PaymentInfo localPaymentInfo = (PaymentInfo)localTicketInfo.getPayments().get(k);
        arrayOfPayment[k] = new Payment();
        arrayOfPayment[k].setAmount(localPaymentInfo.getTotal());
        if ("magcard".equals(localPaymentInfo.getName()))
          arrayOfPayment[k].setPaymentType("K");
        else if ("cheque".equals(localPaymentInfo.getName()))
          arrayOfPayment[k].setPaymentType("2");
        else if ("cash".equals(localPaymentInfo.getName()))
          arrayOfPayment[k].setPaymentType("B");
        else
          arrayOfPayment[k].setPaymentType(null);
      }
      arrayOfOrder[i].setPayment(arrayOfPayment);
    }
    return arrayOfOrder;
  }

  private static int parseInt(String paramString)
  {
    try
    {
      return Integer.parseInt(paramString);
    }
    catch (NumberFormatException localNumberFormatException)
    {
    }
    return 0;
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.possync.OrdersSync
 * JD-Core Version:    0.6.0
 */